package com.outsourcing.bottle.ui;

import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.google.gson.Gson;
import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.GroupEntry;
import com.outsourcing.bottle.domain.GroupInfo;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.UICore;

/**
 * 
 * @author 06peng
 * 
 */
public class ManagerGroupActivity extends BasicActivity implements
		BasicUIEvent, OnClickListener, Callback, OnEditorActionListener {

	private final int init_group_info = 1;
	private final int init_group_info_success = 2;
	private final int error = 3;
	private final int set_group = 4;
	private final int set_group_success = 5;

	private Handler handler;
	private List<GroupEntry> groupList;

	private EditText[] editView = new EditText[8];
	private ImageView[] confirmView = new ImageView[8];
	private ImageView backView;

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.manager_group);
		handler = new Handler(this);
		initWidget();
		UICore.eventTask(this, this, init_group_info, "init_group_info", null);
	}

	private void initWidget() {
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		editView[0] = (EditText) findViewById(R.id.setting_group_edit1);
		editView[1] = (EditText) findViewById(R.id.setting_group_edit2);
		editView[2] = (EditText) findViewById(R.id.setting_group_edit3);
		editView[3] = (EditText) findViewById(R.id.setting_group_edit4);
		editView[4] = (EditText) findViewById(R.id.setting_group_edit5);
		editView[5] = (EditText) findViewById(R.id.setting_group_edit6);
		editView[6] = (EditText) findViewById(R.id.setting_group_edit7);
		editView[7] = (EditText) findViewById(R.id.setting_group_edit8);
		editView[0].setOnEditorActionListener(this);
		editView[1].setOnEditorActionListener(this);
		editView[2].setOnEditorActionListener(this);
		editView[3].setOnEditorActionListener(this);
		editView[4].setOnEditorActionListener(this);
		editView[5].setOnEditorActionListener(this);
		editView[6].setOnEditorActionListener(this);
		editView[7].setOnEditorActionListener(this);

		confirmView[0] = (ImageView) findViewById(R.id.setting_group_confirm1);
		confirmView[1] = (ImageView) findViewById(R.id.setting_group_confirm2);
		confirmView[2] = (ImageView) findViewById(R.id.setting_group_confirm3);
		confirmView[3] = (ImageView) findViewById(R.id.setting_group_confirm4);
		confirmView[4] = (ImageView) findViewById(R.id.setting_group_confirm5);
		confirmView[5] = (ImageView) findViewById(R.id.setting_group_confirm6);
		confirmView[6] = (ImageView) findViewById(R.id.setting_group_confirm7);
		confirmView[7] = (ImageView) findViewById(R.id.setting_group_confirm8);

		confirmView[0].setOnClickListener(this);
		confirmView[1].setOnClickListener(this);
		confirmView[2].setOnClickListener(this);
		confirmView[3].setOnClickListener(this);
		confirmView[4].setOnClickListener(this);
		confirmView[5].setOnClickListener(this);
		confirmView[6].setOnClickListener(this);
		confirmView[7].setOnClickListener(this);
	}

	private void updateUI() {
		try {
			if (groupList != null && !groupList.isEmpty()) {
				editView[0].setText(groupList.get(0).getGroup());
				editView[1].setText(groupList.get(1).getGroup());
				editView[2].setText(groupList.get(2).getGroup());
				editView[3].setText(groupList.get(3).getGroup());
				editView[4].setText(groupList.get(4).getGroup());
				editView[5].setText(groupList.get(5).getGroup());
				editView[6].setText(groupList.get(6).getGroup());
				editView[7].setText(groupList.get(7).getGroup());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case init_group_info_success:
			updateUI();
			break;
		case error:
			onToast((String) msg.obj);
			break;
		case set_group_success:
			onToast((String) msg.obj);
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if (v == backView) {
			finish();
		} else if (v == confirmView[0]) {
			String[] params = new String[2];
			params[0] = String.valueOf(groupList.get(0).getGroupid());
			params[1] = editView[0].getText().toString().trim();
			UICore.eventTask(this, this, set_group, "set_group", params);
		} else if (v == confirmView[1]) {
			String[] params = new String[2];
			params[0] = String.valueOf(groupList.get(1).getGroupid());
			params[1] = editView[1].getText().toString().trim();
			UICore.eventTask(this, this, set_group, "set_group", params);
		} else if (v == confirmView[2]) {
			String[] params = new String[2];
			params[0] = String.valueOf(groupList.get(2).getGroupid());
			params[1] = editView[2].getText().toString().trim();
			UICore.eventTask(this, this, set_group, "set_group", params);
		} else if (v == confirmView[3]) {
			String[] params = new String[2];
			params[0] = String.valueOf(groupList.get(3).getGroupid());
			params[1] = editView[3].getText().toString().trim();
			UICore.eventTask(this, this, set_group, "set_group", params);
		} else if (v == confirmView[4]) {
			String[] params = new String[2];
			params[0] = String.valueOf(groupList.get(4).getGroupid());
			params[1] = editView[4].getText().toString().trim();
			UICore.eventTask(this, this, set_group, "set_group", params);
		} else if (v == confirmView[5]) {
			String[] params = new String[2];
			params[0] = String.valueOf(groupList.get(5).getGroupid());
			params[1] = editView[5].getText().toString().trim();
			UICore.eventTask(this, this, set_group, "set_group", params);
		} else if (v == confirmView[6]) {
			String[] params = new String[2];
			params[0] = String.valueOf(groupList.get(6).getGroupid());
			params[1] = editView[6].getText().toString().trim();
			UICore.eventTask(this, this, set_group, "set_group", params);
		} else if (v == confirmView[7]) {
			String[] params = new String[2];
			params[0] = String.valueOf(groupList.get(7).getGroupid());
			params[1] = editView[7].getText().toString().trim();
			UICore.eventTask(this, this, set_group, "set_group", params);
		}
	}

	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case init_group_info:
			initGroupInfo();
			break;
		case set_group:
			String[] params = (String[]) obj;
			setGroup(params[0], params[1]);
			break;
		default:
			break;
		}
	}

	private void initGroupInfo() {
		StringBuffer url = new StringBuffer(UrlConfig.get_groups_info);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				Gson gson = new Gson();
				GroupInfo info = gson.fromJson(result, GroupInfo.class);
				if (info.getResults().getAuthok() == 0) {
					Message msg = Message.obtain(handler, error, info
							.getResults().getErrmsg());
					handler.sendMessage(msg);
				} else {
					groupList = info.getMygroups_list();
					handler.sendEmptyMessage(init_group_info_success);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setGroup(String groupId, String groupName) {
		String url = UrlConfig.set_groupname;
		HashMap<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("login_uid",
				String.valueOf(AppContext.getInstance().getLogin_uid()));
		paramsMap.put("login_token", AppContext.getInstance().getLogin_token());
		paramsMap.put("groupid", groupId);
		paramsMap.put("groupname", groupName);
		try {
			String result = HttpUtils.doPost(this, url, paramsMap);
			if (result != null) {
				JSONObject obj = new JSONObject(result);
				JSONObject resultobj = obj.getJSONObject("results");
				if (resultobj.getInt("success") == 0) {
					String errormsg = resultobj.getString("errmsg");
					Message msg = Message.obtain(handler, error, errormsg);
					handler.sendMessage(msg);
				} else {
					String successmsg = resultobj.getString("successmsg");
					Message msg = Message.obtain(handler, set_group_success,
							successmsg);
					handler.sendMessage(msg);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if (v == editView[0]) {
			if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
				String[] params = new String[2];
				params[0] = String.valueOf(groupList.get(0).getGroupid());
				params[1] = editView[0].getText().toString().trim();
				UICore.eventTask(this, this, set_group, "set_group", params);
			}
		} else if (v == editView[1]) {
			if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
				String[] params = new String[2];
				params[0] = String.valueOf(groupList.get(1).getGroupid());
				params[1] = editView[1].getText().toString().trim();
				UICore.eventTask(this, this, set_group, "set_group", params);
			}
		} else if (v == editView[2]) {
			if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
				String[] params = new String[2];
				params[0] = String.valueOf(groupList.get(2).getGroupid());
				params[1] = editView[2].getText().toString().trim();
				UICore.eventTask(this, this, set_group, "set_group", params);
			}
		} else if (v == editView[3]) {
			if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
				String[] params = new String[2];
				params[0] = String.valueOf(groupList.get(3).getGroupid());
				params[1] = editView[3].getText().toString().trim();
				UICore.eventTask(this, this, set_group, "set_group", params);
			}
		} else if (v == editView[4]) {
			if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
				String[] params = new String[2];
				params[0] = String.valueOf(groupList.get(4).getGroupid());
				params[1] = editView[4].getText().toString().trim();
				UICore.eventTask(this, this, set_group, "set_group", params);
			}
		} else if (v == editView[5]) {
			if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
				String[] params = new String[2];
				params[0] = String.valueOf(groupList.get(5).getGroupid());
				params[1] = editView[2].getText().toString().trim();
				UICore.eventTask(this, this, set_group, "set_group", params);
			}
		} else if (v == editView[6]) {
			if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
				String[] params = new String[2];
				params[0] = String.valueOf(groupList.get(6).getGroupid());
				params[1] = editView[6].getText().toString().trim();
				UICore.eventTask(this, this, set_group, "set_group", params);
			}
		} else if (v == editView[7]) {
			if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
				String[] params = new String[2];
				params[0] = String.valueOf(groupList.get(7).getGroupid());
				params[1] = editView[7].getText().toString().trim();
				UICore.eventTask(this, this, set_group, "set_group", params);
			}
		}
		return false;
	}
}
